﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YanShu.Entity;

namespace YanShu.Utils
{
    class CommonUtils
    {
        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String ReadConfig(String key)
        {
            try
            {
                String startPath = Application.StartupPath;
                String path = startPath + "\\config.json";
                if (!File.Exists(path))
                {
                    return null;
                }
                String json = File.ReadAllText(path);
                JObject o = JObject.Parse(json);
                return o[key].ToString();
            }
            catch (Exception e)
            {
                e.ToString();
                return null;
            }
        }
        public static List<LanInfo> ReadLanInfosConfig()
        {
            try
            {
                String startPath = Application.StartupPath;
                String path = startPath + "\\config.json";
                if (!File.Exists(path))
                {
                    return null;
                }
                String json = File.ReadAllText(path);
                CommonEntity entity = JsonConvert.DeserializeObject<CommonEntity>(json);
                return entity.GetLanInfos();
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 读取全部配置
        /// </summary>
        /// <returns></returns>
        public static CommonEntity ReadAllConfig()
        {
            try
            {
                String startPath = Application.StartupPath;
                String path = startPath + "\\config.json";
                if (!File.Exists(path))
                {
                    return null;
                }
                String json = File.ReadAllText(path);
                return JsonConvert.DeserializeObject<CommonEntity>(json);
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="json"></param>
        public static void WriteConfig(String json)
        {
            String startPath = Application.StartupPath;
            String path = startPath + "\\config.json";
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(json);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        /// <summary>
        /// 生成MD5
        /// </summary>
        public static string CreateMD5Hash(string input)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return sb.ToString();
        }
        /// <summary>
        /// 校验账户。以字母开头，长度在4-18之间，只能包含字符、数字和下划线。 
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static bool CheckAccount(String arg)
        {
            Regex regFormat = new Regex(@"^[a-zA-Z]\w{3,17}$");
            return regFormat.IsMatch(arg);
        }
        /// <summary>
        /// 校验密码。至少有数字、大小写字母，长度在6-30之间
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static bool CheckPassword(String arg)
        {
            Regex regFormat = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,30}");
            return regFormat.IsMatch(arg);
        }
        public static bool IsEmail(string inputData)
        {
            Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }
        public static bool IsVerifyCode(String verifyCode) {
            Regex regex = new Regex("^\\d{6}$");
            return regex.IsMatch(verifyCode);
        }
    }
}
